package com.xdap.common.utils;

import com.xdap.motor.utils.StringUtils;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.*;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TxtUtil {

    private static Logger logger = Logger.getLogger(TxtUtil.class);




    public static String getResourceData(String javaFilePath) {
        StringBuffer data = new StringBuffer();
        try {

            //考虑到编码格式
            InputStreamReader read = new
                    InputStreamReader(TxtUtil.class.getClassLoader().getResourceAsStream(javaFilePath), "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                data.append(lineTxt);
            }
            read.close();
            logger.info(javaFilePath + "--解析出来的文本为:\n\n" + data);
        } catch (Exception e) {
            logger.error(javaFilePath + "{}：解析报错:{}", e);
            e.printStackTrace();
        }

        return data.toString();

    }


    /**
     * 数据表字段名转换为驼峰式名字的实体类属性名
     * @param tabAttr   数据表字段名
     * @return  转换后的驼峰式命名
     */
    public static String camelize(String tabAttr){
        if(StringUtils.isBlank(tabAttr))
            return tabAttr;
        Pattern pattern = Pattern.compile("(.*)_(\\w)(.*)");
        Matcher matcher = pattern.matcher(tabAttr);
        if(matcher.find()){
            return camelize(matcher.group(1) + matcher.group(2).toUpperCase() + matcher.group(3));
        }else{
            return tabAttr;
        }
    }

    /**
     * 驼峰式的实体类属性名转换为数据表字段名
     * @param camelCaseStr  驼峰式的实体类属性名
     * @return  转换后的以"_"分隔的数据表字段名
     */
    public static String decamelize(String camelCaseStr){
        return StringUtils.isBlank(camelCaseStr) ? camelCaseStr : camelCaseStr.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    /**
    *@auth lishushu
    *@2023/6/15 10:54
    *@desc {根据驼峰法则，将所有_后的字母变成大写}
    **/
    public static String UpperStr(String dataCode) {
        String targetName = "";
        boolean upper = false;
        if (dataCode.contains("_")) {
            String colDataC = "";
            char[] chars = dataCode.toCharArray();
            for (char c : chars) {
                colDataC = c + "";
                if ('_' == c) {
                    upper = true;
                } else {
                    if (upper) {
                        colDataC = colDataC.toUpperCase();
                        upper = false;
                    }
                    targetName += colDataC;
                }
            }
        } else {
            targetName = dataCode;
        }

        return targetName;
    }




}
